﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;


namespace Amarok.Agents
{
	/// <summary>
	/// This type represents the base class for agent implementations. Derived types must implement an agent-specific
	/// constructor that accepts agent environment and agent options. The agent instance is fully operational directly
	/// after construction and can receive and send messages.
	/// 
	/// Inside the agent various overloaded methods can be used to post and publish messages. Posting messages will
	/// send them to the agent instance itself. This can be used to implement workflows or state machines. Publishing 
	/// messages will send them to the outside world, meaning to subscribed agents and/or to a connected message bus, 
	/// which further forwards the messages to connected agents.
	/// </summary>
	public partial class Agent
	{
		#region Subscribe(AgentReference)

		/// <summary>
		/// Subscribes the supplied agent on the publisher. All messages published that match or derive 
		/// from the specified message type and that pass the optional message filter will be forwarded 
		/// to the subscriber.
		/// </summary>
		/// 
		/// <param name="messageType">
		/// The base type of messages for which to subscribe. This type must be assignable to <see cref="Message"/>.</param>
		/// <param name="subscriber">
		/// An agent reference to the agent subscribing for messages.</param>
		/// <param name="messageFilter">
		/// A callback used as content-based filter. Optional, defaults to null.</param>
		/// 
		/// <returns>
		/// An object representing the subscription that can be used to unsubscribe from the publisher, 
		/// or a null value when the advertiser has already be completed.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// A value was passed to a method that did not accept it as a valid argument, because 
		/// the value representing a type is not assignable to the specified target type.</exception>
		protected ICompletable Subscribe(
			Type messageType,
			AgentReference subscriber,
			Predicate<Message> messageFilter = null)
		{
			return mAdvertiser.Subscribe(messageType, subscriber, messageFilter);
		}

		#endregion

		#region Subscribe(Action<Message>)

		/// <summary>
		/// Subscribes the supplied callback on the publisher. All messages published that match or derive 
		/// from the specified message type and that pass the optional message filter will be forwarded 
		/// to the subscriber.
		/// </summary>
		/// 
		/// <param name="messageType">
		/// The base type of messages for which to subscribe. This type must be assignable to <see cref="Message"/>.</param>
		/// <param name="subscriber">
		/// A callback subscribing for messages.</param>
		/// <param name="messageFilter">
		/// A callback used as content-based filter. Optional, defaults to null.</param>
		/// 
		/// <returns>
		/// An object representing the subscription that can be used to unsubscribe from the publisher, 
		/// or a null value when the advertiser has already be completed.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// A value was passed to a method that did not accept it as a valid argument, because 
		/// the value representing a type is not assignable to the specified target type.</exception>
		protected ICompletable Subscribe(
			Type messageType,
			Action<Message> subscriber,
			Predicate<Message> messageFilter = null)
		{
			return mAdvertiser.Subscribe(messageType, subscriber, messageFilter);
		}

		#endregion

		#region Subscribe<T>(AgentReference)

		/// <summary>
		/// Subscribes the supplied agent on the publisher. All messages published that match or derive 
		/// from the specified message type and that pass the optional message filter will be forwarded 
		/// to the subscriber.
		/// </summary>
		/// 
		/// <typeparam name="TMessage">
		/// The base type of messages for which to subscribe. This type must be assignable to <see cref="Message"/>.</typeparam>
		/// 
		/// <param name="subscriber">
		/// An agent reference to the agent subscribing for messages.</param>
		/// <param name="messageFilter">
		/// A callback used as content-based filter. Optional, defaults to null.</param>
		/// 
		/// <returns>
		/// An object representing the subscription that can be used to unsubscribe from the publisher, 
		/// or a null value when the advertiser has already be completed.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected ICompletable Subscribe<TMessage>(
			AgentReference subscriber,
			Predicate<TMessage> messageFilter = null)
			where TMessage : Message
		{
			return mAdvertiser.Subscribe<TMessage>(subscriber, messageFilter);
		}

		#endregion

		#region Subscribe<T>(Action<T>)

		/// <summary>
		/// Subscribes the supplied callback on the publisher. All messages published that match or derive 
		/// from the specified message type and that pass the optional message filter will be forwarded 
		/// to the subscriber.
		/// </summary>
		/// 
		/// <typeparam name="TMessage">
		/// The base type of messages for which to subscribe. This type must be assignable to <see cref="Message"/>.</typeparam>
		/// 
		/// <param name="subscriber">
		/// A callback subscribing for messages.</param>
		/// <param name="messageFilter">
		/// A callback used as content-based filter. Optional, defaults to null.</param>
		/// 
		/// <returns>
		/// An object representing the subscription that can be used to unsubscribe from the publisher, 
		/// or a null value when the advertiser has already be completed.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected ICompletable Subscribe<TMessage>(
			Action<TMessage> subscriber,
			Predicate<TMessage> messageFilter = null)
			where TMessage : Message
		{
			return mAdvertiser.Subscribe<TMessage>(subscriber, messageFilter);
		}

		#endregion

	}
}
